package cn.book.org.controller;

import cn.book.basic.utils.AjaxResult;
import cn.book.basic.utils.PageList;
import cn.book.org.domain.Employee;
import cn.book.org.query.EmployeeQuery;
import cn.book.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 部门controller类
 * @Author: Mr.She
 * @Version: 1.0
 * @Date: 2022/6/23 21:42
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    /**
    * @Title: loadById
    * @Description: 根据Id查询
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/6/24 9:59
    * @Parameters: [id]
    * @Return cn.book.org.domain.Employee
    */
    @GetMapping("/{id}")
    public Employee loadById(@PathVariable("id") Long id) {
        return employeeService.loadById(id);
    }

    /**
     * @Title: loadAll
     * @Description: 查询所有
     * @Author: Mr.She
     * @Version: 1.0
     * @Date:  2022/6/24 9:35
     * @Parameters: []
     * @Return java.util.List<cn.itsource.org.domain.Employee>
     */
    @GetMapping// 请求地址：http://localhost:8080/employee GET请求
    public List<Employee> loadAll(){
        return employeeService.loadAll();
    }

    /**
    * @Title: queryData
    * @Description: 分页查询
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/6/24 10:12
    * @Parameters: [employeeQuery]
    * @Return cn.book.basic.utils.PageList<cn.book.org.domain.Employee>
     * @return cn.book.basic.utils.AjaxResult
    */
    @PostMapping // 可以直接访问
    public PageList<Employee> queryData(@RequestBody EmployeeQuery employeeQuery) {

            PageList<Employee> data = employeeService.queryData(employeeQuery);
            return data;
    }

    /**
    * @Title: remove
    * @Description: 根据Id删除
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/6/24 9:59
    * @Parameters: [id]
    * @Return cn.book.basic.utils.AjaxResult
    */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id) {
        try {
            employeeService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /**
    * @Title: saveOrUpdate
    * @Description: 新增或修改
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/6/24 10:04
    * @Parameters: [employee]
    * @Return cn.book.basic.utils.AjaxResult
    */
    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody Employee employee) {
        try {
            if (employee.getId() != null) {
                employeeService.update(employee);
            } else {
                employeeService.save(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }

    }

    /**
     * @Title: batchDel
     * @Description: 批量删除
     * @Author: Mr.She
     * @Version: 1.0
     * @Date:  2022/6/25 9:47
     * @Parameters: [ids]
     * @Return cn.itsource.basic.utils.AjaxResult
     */
    @PatchMapping
    public AjaxResult batchDel(@RequestBody Long[] ids){
        try {
            employeeService.batchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @GetMapping("/findId/{id}")
    public List<Employee> findId (@PathVariable("id") Long id){
        return employeeService.findId(id);
    }

}
